<?php
// +----------------------------------------------------------------------
// | Created by PhpStorm
// +----------------------------------------------------------------------
// | Date: 2020/6/23 14:24
// +----------------------------------------------------------------------
// | Use : 说明用途，主要方面！
// +----------------------------------------------------------------------
// | Author: Freedom <961449954@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller\banner;

use app\common\controller\AdminController;
use think\App;
use think\facade\Db;
use think\facade\View;

class AdvertMsg extends AdminController{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\banner\Advert');
        $this->storemodel = app('app\common\model\mall\Store');//机构
        $this->articlemodel = app('app\common\model\media\Articles');//文章
        $this->accountmodel = app('app\common\model\mall\Account');//网上课程
    }

    /**
     * 广告位置、标题搜索
     */
    public function get_where($arr=[]){
        $Array=[];
        if(!empty($arr['title'])){
            $Array[] = ['title','like','%'.$arr['title'].'%'];
        }
        if(!empty($arr['position_id'])){
            $Array[] = ['position_id','like','%'.$arr['position_id'].'%'];
        }
        return $Array;
    }

    /**
     *主页面下拉列表
     */
    public function otherData(){
        $position_id = app('app\common\model\banner\Positions');
        $data = $position_id->field('id,name')->select();
        View::assign('data',$data);
    }

    /**
     *添加、修改下拉列表
     */
    public function get_other($get){
        $position_id = app('app\common\model\banner\Positions');
        $data = $position_id->field('id,name')->select();
        if(isset($get['id'])) {
            $ad = app('app\common\model\banner\Advert');
            $type = $ad->field('type,id,position_id')->find($get['id']);
            View::assign('type',$type);

        }
        View::assign('data',$data);
    }

    /**
     *关联模型
     */
    public function get_with(){
        return 'user';
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();

            // 启动事务
            Db::startTrans();
            try {
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }

                $this->validate($post, $rule);

                if($post['type'] == 1){
                    $rt  = $this->storemodel->where('id',$post['bind_id'])->find();
                }
                if($post['type'] == 2){
                    $rt  = $this->articlemodel->where(['id'=>$post['bind_id'],'status'=>1,'type'=>1])->find();
                }
                if($post['type'] == 3){
                    $rt  = $this->accountmodel->where(['id'=>$post['bind_id'],'status'=>1,'type'=>1])->find();
                }
                if(empty($rt)){
                    return json(['code'=>2,'msg'=>'绑定跳转ID不存在，请填写可跳转ID']);
                }

                $post['app_id'] = $this->model->getApp();
                $save = $this->model->save($post);

                // 提交事务
                Db::commit();
            } catch (\Exception $e){
                Db::rollback();
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->find($id);


        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            try {
                $post = $this->request->post();
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }
                $this->validate($post, $rule);
                if($post['type'] == 1){
                    $rt  = $this->storemodel->where('id',$post['bind_id'])->find();
                }
                if($post['type'] == 2){
                    $rt  = $this->articlemodel->where(['id'=>$post['bind_id'],'status'=>1,'type'=>1])->find();
                }
                if($post['type'] == 3){
                    $rt  = $this->accountmodel->where(['id'=>$post['bind_id'],'status'=>1,'type'=>1])->find();
                }
                if(empty($rt)){
                    return json(['code'=>2,'msg'=>'绑定跳转ID不存在，请填写可跳转ID']);
                }


                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error($e->getMessage());
        }
        View::assign('row', $row);
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view('', compact('row'));
    }


}